﻿using LeanApp.Bll;
using LeanApp.Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// CumulativeCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class CumulativeCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CumulativeCtrl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 文件打开的单击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件参数</param>
        private void BtnOpenFile_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "请打开要执行分析的文件";
                openFileDialog.Filter = "Excel文件|*.xlsx|Excel文件|*.xls";
                if (openFileDialog.ShowDialog() == true)
                {
                    string fileName = openFileDialog.FileName;
                    _thisData = ExcelTool.ReadExcel(fileName);
                    if (_thisData.Count <= 0 || _thisData[0] == null || _thisData[0].Count <= 0)
                    {
                        MessageBox.Show("数据读取失败");
                    }
                    else
                    {
                        int rowCount = _thisData.Count;
                        int colCount = _thisData[0].Count;
                        txtExcelBrief.Text = $@"读入：{rowCount}行，{colCount}列";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 开始分析的单击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件参数</param>
        private void BtnStart_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_thisData == null || _thisData.Count <= 0)
                {
                    MessageBox.Show("导入数据出错");
                }
                _bll.AnalysisData(_thisData);
                CumulativeDataCtrl dataCtrl = new CumulativeDataCtrl("山积图数据", _bll);
                if (dataCtrlContainer.Children.Count > 0)
                {
                    dataCtrlContainer.Children.Clear();
                }
                dataCtrlContainer.Children.Add(dataCtrl);
                //QualityCtrlResultContainer
                CumulativeBarCtrl chartCtrl = new CumulativeBarCtrl("山积图", _bll);
                if (resultCtrlContainer.Children.Count > 0)
                {
                    resultCtrlContainer.Children.Clear();
                }
                resultCtrlContainer.Children.Add(chartCtrl);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 解析所得的数据
        /// </summary>
        private List<Dictionary<string, object>> _thisData = new List<Dictionary<string, object>>();

        /// <summary>
        /// 累积分析业务
        /// </summary>
        private CumulativeBll _bll = new CumulativeBll();
    }
}
